package test3;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

import javax.swing.JOptionPane;

public class Receive {
    //接收端
    public static int n = 0;

    public Receive() throws Exception {
        //1.使用	DatagramSocket指定端口,创建接收端
        DatagramSocket c = new DatagramSocket(8081);//之前指定的接口,如果不一样会丢失数据
        //2.准备容器,封装成Datagram
        byte[] b = new byte[1024];
        DatagramPacket p = new DatagramPacket(b, 0, b.length);
        //3.阻塞方式接受包裹
        c.receive(p);
        //4.分析数据
        byte[] D = p.getData();
        n = D.length;
        String str = new String(D, 0, n);
        JOptionPane.showMessageDialog(null, str);
        //5.示范资源
        c.close();

    }
}

